package dataStruct.queue.ex2;

/**
 * Created by thinkpad on 2016/8/24.
 */
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class Tester {

    public static void printQ(Queue queue) {
        while(queue.peek() != null)
            System.out.print(queue.remove() + " ");
        System.out.println();
    }

    public static void test(){
        // 队列
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);
        ExecutorService service = Executors.newCachedThreadPool();

        for (int i = 0; i < 6; i++) {
            service.submit(new Producer(queue, "ProducerA" + i));
//            service.submit(new Producer(queue, "ProducerB" + i));
        };
        for (int i = 0; i < 6; i++) {
            service.submit(new Consumer(queue, "ConsumerA" + i));
//            service.submit(new Consumer(queue, "ConsumerB" + i));
        }

        service.shutdown();
    }

    public static void test1ProducerConsumer1(){
        // 队列
        LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);
        Producer producer = new Producer(queue, "ProducerA");
        Consumer consumer = new Consumer(queue, "ConsumerA");
        Thread producerThread = new Thread(producer);
        Thread consumerThread = new Thread(consumer);
        producerThread.start();
        try {
            Thread.sleep(2000);//生产需要时间
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        consumerThread.start();
    }

    public static void main(String[] args) {
//        test();
        test1ProducerConsumer1();
    }
}